

let UserName = document.querySelector(".phone :nth-child(2)>input");
let UserMessage = document.querySelector(".phone div>span");
let Usergou = document.querySelector(".phone >span");
let registeredBnt = document.querySelector(".but");
let password = document.querySelector(".create-password div>input");

let passwordMessage = document.querySelector(".create-password>div>span");
let passwordHML = document.querySelector(".pwdInfo");
let passwordquer = document.querySelector(".password div>input");
let ppp = document.querySelector(".password>:nth-child(2) span");
let chcekbox = document.querySelector(".chck div>input")


let userStatus = false;
let pwdStatus = false;
let code = false;
let repeatPwd = false;
let checkboxss = false;

chcekbox.onchange = function(){
	if(!chcekbox.checked){
		checkboxss = false;
		registeredbutton();
		return;
	}
	checkboxss = true;
	registeredbutton();
}

function StatusError(objMsg,objSuc,message){
	objMsg.style.display = 'block';
	objSuc ? objSuc.style.display = 'none' : null;
	objMsg.innerHTML = message;
}
function StatusSuccess(objMsg,objSuc){
	objMsg.style.display = 'none';
	objSuc ? objSuc.style.display = 'block' : null;
	// objMsg.innerHTML = message;
}

let flushed = document.querySelector(".flushed");

flushed.onclick = function(){
	chackCode()
}


chackCode()
function chackCode(){
	
	let oF = document.querySelector(".f");
	let oS = document.querySelector(".s");
	let oT = document.querySelector(".t");
	let oL = document.querySelector(".l");
	
	
	let codelength = "qwertyuiopasdfghjklzxcvbnm123456789";
	
	let f = codelength[parseInt(Math.random()*35)];
	
	let s = codelength[parseInt(Math.random()*35)];
	
	let t = codelength[parseInt(Math.random()*35)];
	
	let l = codelength[parseInt(Math.random()*35)];
	
	oF.innerHTML = f;
	
	oS.innerHTML = s;
	
	oT.innerHTML = t;
	
	oL.innerHTML = l;
	

	oF.style.color = `rgb(${parseInt(Math.random()*256)},${parseInt(Math.random()*256)},${parseInt(Math.random()*256)})`;
	oS.style.color = `rgb(${parseInt(Math.random()*256)},${parseInt(Math.random()*256)},${parseInt(Math.random()*256)})`;
	oT.style.color = `rgb(${parseInt(Math.random()*256)},${parseInt(Math.random()*256)},${parseInt(Math.random()*256)})`;
	oL.style.color = `rgb(${parseInt(Math.random()*256)},${parseInt(Math.random()*256)},${parseInt(Math.random()*256)})`;
	
	console.log(f + s + t +l);
	
	
	let inputcode = document.querySelector(".code");
	let checkSpan = document.querySelector(".chack-code-span");
	inputcode.onchange = function(){
		if(this.value.trim() == f + s + t + l){
			code = true;
			console.log(code)
			StatusSuccess(checkSpan, null)
			registeredbutton()
			
		}else{
			
			StatusError(checkSpan, null, '验证码错误');
			code = false;
			registeredbutton()
			chackCode()
		};
	};
	
	
	
	
}
function registeredbutton(){
	if(userStatus && pwdStatus && checkboxss && code){
	    console.log("www")
	    registeredBnt.disabled = false;
		registeredBnt.style.opacity = 1;
		//注册
		registeredBnt.onclick = function(){
			json.post(URL + '/api_user',{
				username : UserName.value.trim(),
				password : password.value.trim(),
				status : 'register'
			},even =>{
				
				if(even.code == 1005){
					console.log(even);
					return;
				};
				alert('注册成功, 点击确定跳转登录');
				//js 跳转
				window.location.href = 'login.html';
			})
		};
	}else{
	
		registeredBnt.disabled = true;
		registeredBnt.style.opacity = 0.7;
	}
}

//校验用户名
UserName.onblur = function (){
	let userValue = this.value.trim();
	// console.log(userValue)
	
	let usernameCheck = /^[0-9a-z_A-Z]{3,12}$/g;
	
	if(!usernameCheck.test(userValue)){
		StatusError(UserMessage,Usergou, "用户名必须由3~12位字母数字下划线组成");
	
		userStatus = false;
		registeredbutton();
		return;
	};
	
	json.post(URL + '/api_user', {
		username: userValue,
		status : 'check'
	}, even =>{
		// console.log(even)
		if(even.code == 1005){
			StatusError(UserMessage,Usergou,"用户名已存在");
			userStatus = false;
			registeredbutton();
			return;
		}
		
	})
	StatusSuccess(UserMessage,Usergou);
	userStatus = true;
	registeredbutton()

};
//校验密码
password.oninput = function(){
	
	let passwordValue = this.value.trim();
	
	let pwdReg = /^\d{6,12}$/g;
	
	if(!pwdReg.test(passwordValue)){
		
		StatusError(passwordMessage, null, '密码为3-12位数字');
			strengthPwd();
			pwdStatus = false;
			registeredbutton()
		return;
	}
	
	     StatusSuccess(passwordMessage, null);
		//密码强度
		if(passwordValue.length >= 6 && passwordValue.length < 8){
			strengthPwd('弱');
		}else if(passwordValue.length >= 8 && passwordValue.length < 10){
			strengthPwd('中');
		}else{
			strengthPwd('强');
		}
	let password =  passwordquer.value;	

		
};
//确认密码
passwordquer.oninput = function(){
	let passwordduibi =  this.value.trim();	

	if(passwordduibi != password.value.trim()){
		StatusError(ppp, null, '确认密码和创建密码不同');
		pwdStatus = false;
		registeredbutton()
		return;
	}
    StatusSuccess(ppp, null);
	pwdStatus = true;
	registeredbutton();
}


//密码强度
function strengthPwd (status = ""){

	switch(status){
		case '弱':
		   passwordHML.style.backgroundImage = " url(http://imgs-qn.iliangcang.com/images/default/pwd_note_bg_low.png)"
			break;
		case '中':
		    passwordHML.style.backgroundImage = " url(http://imgs-qn.iliangcang.com/images/default/pwd_note_bg_mid.png)"
			break;
		case '强':
		     passwordHML.style.backgroundImage = " url(http://imgs-qn.iliangcang.com/images/default/pwd_note_bg_high.png)"	
			break;
		default:
			passwordHML.style.backgroundImage = "url(http://imgs-qn.iliangcang.com/images/default/pwd_note_bg.png)"	
			break;
	};
}